import BetterUtilTools from 'better-util-tools';
import store from '@/store';
import { appkey } from '@/assets/config.js';
import CryptoJS from 'crypto-js';
const { state } = store;
const UtilTools = new BetterUtilTools();

const utils = {
	UtilTools,
	getCurPage() {
		let pages = getCurrentPages();
		let page = pages[pages.length - 1];
		return page;
	},
	checkLogin() {
		const { userInfo } = state;
		return new Promise((resolve, reject) => {
			if (userInfo.memberinfo.mobile) {
				resolve();
			} else {
				reject();
			}
		});
	},
	showLoading(opts) {
		return uni.showLoading(UtilTools.extend({
			'title': '拼命加载中...',
			'mask': true
		}, opts));
	},
	hideLoading() {
		uni.hideLoading();
	},
	showToast(opts) {
		uni.hideLoading();
		uni.showToast(UtilTools.extend({
			'icon': 'none',
			'duration': 2000,
			'title': ''
		}, opts));
	},
	showModal(opts) {
		opts = {
			'content': opts.title || opts.content
		};
		uni.showModal(UtilTools.extend({
			'title': '提示',
			'showCancel': false,
			'confirmText': '知道了'
		}, opts));
	},
	// 参数加密
	aesEncrypt(data, key = appkey) {
		return CryptoJS.AES.encrypt(JSON.stringify(data), key).toString();
	},
	// 参数解密
	aesDecrypt(text, key = appkey) {
		const bytes = CryptoJS.AES.decrypt(text, key);
		try {
			return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
		} catch (e) {
			return {};
		}
	}
};

export default utils;